package com.suishoupai.xmb.controller;

import com.suishoupai.xmb.entity.*;
import com.suishoupai.xmb.exception.XmbException;
import com.suishoupai.xmb.service.AuthService;
import com.suishoupai.xmb.service.UserService;
import com.suishoupai.xmb.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

/**
 * Created by caojian on 2017/4/7.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private AuthService authService;
    @Autowired
    private UserService userService;
    /**
     * 忘记密码页面
     *
     * @param message
     * @param model
     * @return
     */
    @GetMapping("/forgetPsd")
    @PreAuthorize("permitAll()")
    public Object forgetPsdPage(@ModelAttribute("message") String message, Model model) {
        model.addAttribute("message", message);
        return "forget-password";
    }

    /**
     * 忘记密码
     *
     * @param email
     * @param model
     * @return
     */
    @PostMapping("/forgetPsd")
    @PreAuthorize("permitAll()")
    public Object forgetPsd(String email, RedirectAttributes model) {
        ValidateUtil.isEmail(email);
        if (!authService.hasAuth(email, AuthTypeEnum.RESET_PSD.getValue())) {
            try {
                authService.addResetPsd(email);
            } catch (XmbException e) {
                model.addAttribute("message", e.getError());
                return new ModelAndView(new RedirectView("forgetPsd", true, false, true));
            }
        } else {
            try {
                authService.reResetPsd(email);
            } catch (XmbException e) {
                model.addAttribute("message", e.getError());
                return new ModelAndView(new RedirectView("forgetPsd", true, false, true));
            }
        }
        model.addAttribute("message", "邮件已发送，请注意查收。");
        return new ModelAndView(new RedirectView("forgetPsd", true, false, true));
    }

    /**
     * 重置密码页面
     *
     * @param message
     * @param auth
     * @param model
     * @return
     */
    @GetMapping("/resetPsd")
    @PreAuthorize("permitAll()")
    public Object resetPsdPage(@ModelAttribute("message") String message, Auth auth, Model model) {
        auth.setType(AuthTypeEnum.RESET_PSD.getValue());
        model.addAttribute("message", message);
        model.addAttribute("email", auth.getEmail());
        model.addAttribute("token", auth.getToken());
        if (!authService.validate(auth)) {
            model.addAttribute("message", "该链接已失效。");
        }
        return "reset-password";
    }

    /**
     * 重置密码
     *
     * @param userResetPsd
     * @param model
     * @return
     */
    @PostMapping("/resetPsd")
    @PreAuthorize("permitAll()")
    public Object resetPsd(UserResetPsd userResetPsd, RedirectAttributes model) {
        ValidateUtil.min(userResetPsd.getNewPassword(), 6);

        Auth auth = userResetPsd.getAuth();
        auth.setType(AuthTypeEnum.RESET_PSD.getValue());
        String password = userResetPsd.getNewPassword();
        if (!authService.validate(auth)) {
            return new ModelAndView(new RedirectView("resetPsd", true, false, true));
        }
        userService.resetPsd(auth.getEmail(), password);
        model.addAttribute("message", "密码已重置，请保管好你的密码。");
        return new ModelAndView(new RedirectView("/login", true, false, true));
    }
}
